﻿using System;
using System.Collections.Generic;
using System.Reflection;

using ActiveDocument.Attributes;

using Lucene.Net.Documents;

namespace ActiveDocument.Persistence
{
    public class ActiveDocumentConverter
    {
        public static Document ConvertToDocument(Object obj)
        {
            Document d = new Document();
            d.Add(new Field("type", obj.GetType().Name, Field.Store.YES, Field.Index.UN_TOKENIZED));
            foreach (PropertyInfo inf in obj.GetType().GetProperties())
            {
                object[] att = inf.GetCustomAttributes(false);
                for (int i = 0; i < att.Length; i++)
                    if (att[i].GetType() == typeof(IndexedAttribute))
                        d.Add(new Field(inf.Name, inf.GetValue(obj, null).ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
            }
            return d;
        }
    }
}